/**
  ******************************************************************************
  * @file    kea128_adc.c
  * @author  huaxicunzhang
  * @version V1.0
  * @date    18-8-2015
  * @brief   This file contains all the functions prototypes for the GPIO 
  *          firmware library.
  ******************************************************************************
**/ 
#include "kea128_adc.h"

/**
  * @brief  ADC init
  * @param  CHANNELx: Select the CHANNEL0 or the CHANNEL1 peripheral. 
  * @param  NewState: new state of the specified USARTx interrupts.
  *   This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void ADC_Init(ADC_InitTypeDef *ADC_InitStruct)
{
	ASSERT(IS_ADC_CHANNEL(ADC_InitStruct->ADC_CHx));
	
	ASSERT(IS_ADC_nbit(ADC_InitStruct->ADC_Mode));
	
	SIM->SCGC |= SIM_SCGC_ADC_MASK;  //打开ADC模块时钟
	
  //	ADC最大8M 采集内部最大1M
	ADC->SC3 |= ADC_SC3_ADICLK(ADC_InitStruct->ADC_CLK); //ADC时钟选择
	
	ADC->SC3 |= ADC_SC3_ADIV(ADC_InitStruct->ADC_ADIV); //ADC分频
	
	ADC->SC3 |=ADC_SC3_MODE(ADC_InitStruct->ADC_Mode); //ADC采样精度选择
	
	ADC->SC4 |=ADC_SC4_AFDEP(ADC_InitStruct->ADC_FIFO); //Set ADC FIFO
	
	ADC->APCTL1 = ADC_APCTL1_ADPC(1<<ADC_InitStruct->ADC_CHx); //Select Control Pin
	
	ADC->SC1 |=ADC_SC1_ADCH(ADC_InitStruct->ADC_CHx); //ADC通道选择
	
	ADC->SC2 |=ADC_SC2_REFSEL(0);          //reference volage is Vrefh 
	
	if(ADC_InitStruct->ADC_Trigger == HardwareTrigger)
	ADC->SC2 |=ADC_SC2_ADTRG_MASK;
	else
	ADC->SC2 &=~ADC_SC2_ADTRG_MASK;
	
}



/*
  ******************************************************************************
	* 
	*             ADC转换完成中断使能
	******************************************************************************
 */
void ADC_ITConfig(ADC_Type* pADC,FunctionalState NewState)
{
	
	if(NewState != DISABLE)
	{
		pADC->SC1 |= ADC_SC1_AIEN_MASK;
	}
	else
	 pADC->SC1 &= ~ADC_SC1_AIEN_MASK ;
	
}

/*
  ******************************************************************************
	* 
	*             ADC转换结果
	*    
	******************************************************************************
 */
uint16_t ADC_GetConversionValue(ADCHn ch)
{
	uint16_t result = 0;
	//设置SC1寄存器通道号
  ADC_SC1 = (ADC_SC1 & ~ADC_SC1_ADCH_MASK) | ADC_SC1_ADCH(ch);
	//等待转换完成
  while(!(ADC_SC1 & ADC_SC1_COCO_MASK));
	//读取转换结果
	result = ADC->R; 
	//清ADC转换完成标志
	ADC->SC1 &= ~ADC_SC1_COCO_MASK;
  
  return result;
}


 void ADC_Init1( ADCHn  ch,ADC_nbit bit)
{
   SIM_SCGC |= SIM_SCGC_ADC_MASK;   //?????
   ADC_SC3  = (0
                      |ADC_SC3_ADICLK(0)    //??????
                      |ADC_SC3_MODE(bit)            //8?AD??
                      //|ADC_SC3_ADLSMP_MASK         //?????
                      |ADC_SC3_ADIV(0)      //???1
                      //|ADC_SC3_ADLPC_MASK            //?????
                    ) ;
    
   ADC_SC2  = (0
                     // |ADC_SC2_ADTRG_MASK             //1????,0????
                    //  |ADC_SC2_ACFE_MASK            //????
                    //  |ADC_SC2_ACFGT_MASK           //?????,????
                    ) ;
       ADC_APCTL1 = ADC_APCTL1_ADPC(1<< ch) ; 
       ADC_SC1  = (0
                    |ADC_SC1_ADCH(ch)             //??????
                   // |ADC_SC1_ADCO_MASK            //????
                   // |ADC_SC1_AIEN_MASK           //??
                    ) ;  
}


u16 adc_once(ADCHn adcn_ch, ADC_nbit bit) //????????AD?
{

  
  u16 result = 0;
  
//  adc_start(adcn_ch, bit);      //??ADC??
   ADC_Init1( adcn_ch , bit) ;    //??ADC??

  while ((ADC_SC1 & ADC_SC1_COCO_MASK ) != ADC_SC1_COCO_MASK);   //??? A??
  result = ADC_R;
  ADC_SC1 &= ~ADC_SC1_COCO_MASK;
  return result;
}



